<template>
	<view :style="{paddingTop:navHeight}">
		<view class="top-nav-box" :style="{height:navHeight, paddingTop:statusBarHeight}">
			<image class="top-nav-home" src="../../../static/images/icon_float_home.png" @click="goHome"></image>
			<text class="top-nav-title">领取优惠券</text>
			<view style="width: 58rpx;"></view>
		</view>
		<view class="coupon-receive-wrap" :style="{top:navHeight}">
			<image :src="scanImg" mode="widthFix" style="width: 100%;" @click="getScanCouponFun()"></image>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters
	} from 'vuex';
	import {
		strToParams
	} from "@/utils/tools";
	import {
		getScanCouponInfo,
		getScanCoupon
	} from "@/api/activity";
	import {
		QUERY_CODE
	} from "@/config/cachekey";
	import Cache from "@/utils/cache";
	import {
		GetWxMiniProgramUrlParam
	} from '@/utils/tools'
	export default {
		data() {
			return {
				code: '', //201e284c3e531c4f0e
				scanImg: '',
				getScanCouponKey: true
			}
		},
		computed: {
			...mapGetters(['sysInfo']),
			statusBarHeight() {
				return this.sysInfo.statusBarHeight + 'px'
			},
			navHeight() {
				return this.sysInfo.navHeight + 'px'
			},
			marginTop() {
				return '-' + (this.sysInfo.navHeight) + 'px'
			},
		},
		async onLoad(options) {
			await this.$onLaunched;

			let opt = Cache.get(QUERY_CODE);
			if (opt.q) {
				const url = decodeURIComponent(opt.q);
				let params = GetWxMiniProgramUrlParam(url); //获取二维码路径参数
				this.code = params.code;
			} else {
				this.code = opt.code;
			}
			this.getScanCouponInfoFun();
		},
		methods: {
			getScanCouponInfoFun() {
				getScanCouponInfo(this.code).then((res) => {
					if (res.code == 1) {
						this.scanImg = res.data.get_back_image;
					} else {
						this.$toast({
							title: res.msg
						});
					}
				});
			},
			getScanCouponFun() {
				if (this.getScanCouponKey) {
					this.getScanCouponKey = false;
					this.$toast({
						title: "正在领券中，请稍后..."
					});
					getScanCoupon(this.code).then((res) => {
						this.$toast({
							title: res.msg
						});
						this.getScanCouponKey = true;
					});
				}
			},
			goHome() {
				uni.switchTab({
					url: "/pages/index/index"
				});
			}
		}
	}
</script>

<style lang="scss">
	.top-nav-box {
		position: fixed;
		display: flex;
		align-items: center;
		justify-content: space-between;
		width: 100%;
		padding: 0 32rpx;
		top: 0;
		z-index: 1;

		.top-nav-home {
			width: 58rpx;
			height: 58rpx;
		}

		.top-nav-title {
			flex: 1;
			font-size: 28rpx;
			font-weight: 700;
			color: #303030;
			text-align: center;
		}
	}
</style>